package br.com.fiap.javaMasters.util;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author yawit
 * Banco de dados aqui da YaW modificar para o seu Banco
 */
public class Connection {
    public static Logger log = Logger.getLogger(Connection.class.toString());
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://";
    private static final String USERNAME = "avaliacao";
    private static final String PASSWORD = "avaliacao";
    private static final String IP = "192.168.1.18";
    private static final String PORT = "3306";
    private static final String DATABASE = "avaliacao";

    public static Connection getConnection() throws ClassNotFoundException,SQLException {
        Class.forName(DRIVER);
        log.log(Level.INFO, "Buscando Conexao no SQLServer");

        java.sql.Connection c = DriverManager.getConnection(URL + IP + ":"
                + PORT + "/" + DATABASE, USERNAME, PASSWORD);
        c.setAutoCommit(false);
        return (Connection) c;
    }
    public static void closeAll(Connection c) {
        if (c != null)
            try {
                ((Statement) c).close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    public static void closeAll(Connection c, PreparedStatement stm) {
        closeAll(c);
        closeAll(stm);
    }
    public static void closeAll(Connection c, PreparedStatement stm,
            ResultSet rs) {
        closeAll(c);
        closeAll(stm);
        closeAll(rs);
    }
    public static void closeAll(PreparedStatement stm) {
        if (stm != null)
            try {
                stm.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    public static void closeAll(PreparedStatement stm, ResultSet rs) {
        closeAll(stm);
        closeAll(rs);
    }
    public static void closeAll(ResultSet rs) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    public static void closeAll(Connection c, ResultSet rs) {
        closeAll(c);
        closeAll(rs);
    }
}
